package cn.edu.cqu.fredyvia.forum.service.impl;

import cn.edu.cqu.fredyvia.forum.dto.TalkUserinfoDTO;
import cn.edu.cqu.fredyvia.forum.mapper.TalkMapper;
import cn.edu.cqu.fredyvia.forum.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    TalkMapper talkMapper;

    @Override
    public List<TalkUserinfoDTO> search(String key, Integer from, Integer to) {
        List<TalkUserinfoDTO> list = talkMapper.select_inject(from, to, "total_comment");
        List<TalkUserinfoDTO> result = new ArrayList<>();
        Pattern p = Pattern.compile(key);
        for (TalkUserinfoDTO talkUserinfoDTO : list) {
            Matcher m1 = p.matcher(talkUserinfoDTO.getUsername());
            Matcher m2 = p.matcher(talkUserinfoDTO.getTitle());
            if (m1.find() || m2.find()) {
                result.add(talkUserinfoDTO);
            }
        }
        return result;
    }
}
